/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of the Maxsi Library.

	Maxsi Library is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Library. If not, see <http://www.gnu.org/licenses/>.

	Maxsi Library
	A powerful Cross-Platform C++ General Purpose Library that allows you to
	efficiently create high-performance and reliable applications.

	MaxsiTLS.h
	A buffer streamer on a TLS (the successor to SSL) connection, which is a
	secure encrypted reliable transport protocol layer.

******************************************************************************/

#ifndef MaxsiTLS_H
#define MaxsiTLS_H

#ifdef Maxsi_Support_GNUTLS
#include <gnutls/gnutls.h>
#endif

BeginMaxsiNamespace

//=============================================================================
//	ITLS is an interface.
//=============================================================================
#undef MAXSI_INTERFACE
#define MAXSI_INTERFACE MAXSI_INTERFACE_ABSTRACT_BASECLASS

//=============================================================================
//	ITLS - An interface to TLS connection management.
//=============================================================================
MAXSI_CLASSLINK ITLS : public ITCP
{
	MAXSI_DECLARE_INTERFACE(ITLS);

public:
	virtual IBufferStreamer* Connect(MESTR* HostName, short Port, IBufferStreamer* Proxy = NULL) MAXSI_INTERFACE;
	virtual void CloseConnection(IBufferStreamer* Connection) MAXSI_INTERFACE;

};

//=============================================================================
//	ITLSStreamer is an interface.
//=============================================================================
#undef MAXSI_INTERFACE
#define MAXSI_INTERFACE MAXSI_INTERFACE_ABSTRACT_BASECLASS

//=============================================================================
//	ITLSStreamer - A buffer streamer for TLS connections.
//=============================================================================
MAXSI_CLASSLINK ITLSStreamer : public ITCPStreamer
{
	MAXSI_DECLARE_INTERFACE(ITLS);

public:
	virtual MaxsiError SetProxyInformation(IBufferStreamer* NewProxy, MESTR* HostName, short Port) MAXSI_INTERFACE;

};

#ifdef Maxsi_Support_GNUTLS
//=============================================================================
//	MaxsiGNUTLS is implemented.
//=============================================================================
#undef MAXSI_INTERFACE
#define MAXSI_INTERFACE MAXSI_INTERFACE_IMPLEMENTED_IN_MAXSI_LIBRARY

//=============================================================================
//	MaxsiGNUTLS uses GnuTLS for encrypted connections.
//=============================================================================
MAXSI_CLASSLINK MaxsiGNUTLSStreamer : public ITLSStreamer
{
	MAXSI_DECLARE_IMPLEMENTATION(MaxsiGNUTLSStreamer, ITLSStreamer, MAXSI_INTERFACE_DYNAMIC);

public:
	MaxsiGNUTLSStreamer();
	virtual ~MaxsiGNUTLSStreamer();

public:
	gnutls_certificate_credentials_t	CertificateCredentials;
	bool								CertificateCredentialsInitialized;
	gnutls_session_t					Session;
	bool								SessionInitialized;

public:
	virtual MaxsiError SetProxyInformation(IBufferStreamer* NewProxy, MESTR* HostName, short Port) MAXSI_INTERFACE;
	virtual void SetConnectionHandle(MaxsiHandle Handle) MAXSI_INTERFACE;
	virtual MaxsiHandle GetConnectionHandle() MAXSI_INTERFACE;

public:
	virtual int GetFlags() MAXSI_INTERFACE;
	virtual FileSize_t GetSize() MAXSI_INTERFACE;
	virtual MaxsiError SetSize(FileSize_t NewSize) MAXSI_INTERFACE;
	virtual FileSize_t GetPosition() MAXSI_INTERFACE;
	virtual MaxsiError Seek(FileSize_t Offset) MAXSI_INTERFACE;
	virtual MaxsiError Read(BYTE* Destination, size_t DestinationSize, size_t* ActualRead) MAXSI_INTERFACE;
	virtual MaxsiError ReadFrom(FileSize_t Offset, BYTE* Destination, size_t DestinationSize, size_t* ActualRead) MAXSI_INTERFACE;
	virtual MaxsiError Write(BYTE* Source, size_t SourceSize) MAXSI_INTERFACE;
	virtual MaxsiError WriteTo(FileSize_t Offset, BYTE* Source, size_t SourceSize) MAXSI_INTERFACE;
	virtual MaxsiError WriteFlush() MAXSI_INTERFACE;
	
};

//=============================================================================
//	MaxsiGNUTLS is implemented.
//=============================================================================
#undef MAXSI_INTERFACE
#define MAXSI_INTERFACE MAXSI_INTERFACE_IMPLEMENTED_IN_MAXSI_LIBRARY

//=============================================================================
//	Used to create new GnuTLS connections.
//=============================================================================
MAXSI_CLASSLINK MaxsiGNUTLS : public ITLS
{
	MAXSI_DECLARE_IMPLEMENTATION(MaxsiGNUTLS, ITLS, MAXSI_INTERFACE_STATIC);


public:
	MaxsiGNUTLS();
	virtual ~MaxsiGNUTLS();

public:
	virtual IBufferStreamer* Connect(MESTR* HostName, short Port, IBufferStreamer* Proxy = NULL) MAXSI_INTERFACE;
	virtual void CloseConnection(IBufferStreamer* Connection) MAXSI_INTERFACE;

};
#endif

EndMaxsiNamespace

#endif

